;;; Mudsync --- Live hackable MUD
;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
;;;
;;; This file is part of Mudsync.
;;;
;;; Mudsync is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Mudsync is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Mudsync.  If not, see <http://www.gnu.org/licenses/>.

;;; Game actor
;;; ==========

(define-module (mudsync gameobj)
  #:use-module (mudsync command)
  #:use-module (mudsync utils)
  #:use-module (mudsync receive-star)
  #:use-module (8sync actors)
  #:use-module (8sync agenda)
  #:use-module (8sync rmeta-slot)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 control)
  #:use-module (ice-9 format)
  #:use-module (ice-9 match)
  #:use-module (oop goops)
  #:export (<gameobj>

            create-gameobj
            gameobj-loc
            gameobj-gm
            gameobj-desc

            gameobj-act-init
            gameobj-set-loc!
            gameobj-occupants
            gameobj-self-destruct

            slot-ref-maybe-runcheck
            val-or-run

            build-props
            dyn-ref

            ;; Some of the more common commands
            cmd-look-at
            cmd-take cmd-drop
            cmd-take-from-no-op cmd-put-in-no-op))

;;; Gameobj
;;; =======

(define build-props build-rmeta-slot)

;;; *all* game components that talk to players should somehow
;;; derive from this class.
;;; And all of them need a GM!

(define-class <gameobj> (<actor>)
  ;; location id
  (loc #:init-value #f
       #:getter gameobj-loc)
  
  ;; Uses a hash table like a set (values ignored)
  (occupants #:init-thunk make-hash-table)

  ;; game master id
  (gm #:init-keyword #:gm
      #:getter gameobj-gm)

  ;; a name to be known by
  (name #:init-keyword #:name
        #:init-value #f)
  (goes-by #:init-keyword #:goes-by
           #:init-value #f)

  (desc #:init-value #f
        #:init-keyword #:desc)

  ;; @@: Maybe commands should be renamed to verbs, I dunno
  ;; Commands we can handle
  (commands #:allocation #:each-subclass
            #:init-thunk (build-commands
                          (("l" "look") ((direct-command cmd-look-at)))
                          ("take" ((direct-command cmd-take)
                                   (prep-indir-command cmd-take-from
                                                       '("from" "out of"))))
                          ("put" ((prep-indir-command cmd-put-in
                                                      '("in" "inside" "into" "on"))))))

  ;; Commands we can handle by being something's container
  ;;   dominant version (goes before everything)
  (container-dom-commands #:allocation #:each-subclass
                          #:init-thunk (build-commands))
  ;;   subordinate version (goes after everything)
  (container-sub-commands #:allocation #:each-subclass
                          #:init-thunk (build-commands))

  ;; Commands we can handle by being contained by something else
  (contained-commands #:allocation #:each-subclass
                      #:init-thunk 
                      (build-commands
                       (("l" "look") ((direct-command cmd-look-at)))
                       ("drop" ((direct-command cmd-drop #:obvious? #f)))))

  ;; The extremely squishy concept of "props"... properties!
  ;; These are flags, etc etc of various types.  This is a hashq table.
  ;; These have upsides and downsides, but the big upside is that you can
  ;; query a "prop" of a prospective gameobj without knowing what type of
  ;; gameobj that is, and not fear some kind of breakage.
  ;;
  ;; props by default only have a 'get-prop read-only action handler;
  ;; any coordination of setting a prop between actors must be
  ;; added to that actor, to keep things from getting out of control.
  (props #:init-thunk make-hash-table
         #:init-keyword #:props)
  ;; gameobjs may inherit an initial list of these via the
  ;; initial-props slot, which must always have its
  ;; #:allocation #:each-subclass and use (build-props) for the
  ;; #:init-thunk.
  ;; The vanilla gameobj has no props, on purpose.
  (initial-props #:allocation #:each-subclass
                 #:init-thunk (build-props '()))

  ;; Most objects are generally visible by default
  (invisible? #:init-value #f
              #:init-keyword #:invisible?)
  ;; TODO: Fold this into a procedure in invisible? similar
  ;;   to take-me? and etc
  (visible-to-player?
   #:init-value (wrap-apply gameobj-visible-to-player?))

  ;; Can be a boolean or a procedure accepting
  ;; (gameobj whos-acting #:key from)
  (take-me? #:init-value #f
            #:init-keyword #:take-me?)
  ;; Can be a boolean or a procedure accepting
  ;; (gameobj whos-acting where)
  (drop-me? #:init-value #t
            #:init-keyword #:drop-me?)

  ;; TODO: Remove this and use actor-alive? instead.
  ;; Set this on self-destruct
  ;; (checked by some "long running" game routines)
  (destructed #:init-value #f)

  (actions #:allocation #:each-subclass
           ;;; Actions supported by all gameobj
           #:init-thunk
           (build-actions
            (init gameobj-act-init)
            ;; Commands for co-occupants
            (get-commands gameobj-get-commands)
            ;; Commands for participants in a room
            (get-container-dom-commands gameobj-get-container-dom-commands)
            (get-container-sub-commands gameobj-get-container-sub-commands)
            ;; Commands for inventory items, etc (occupants of the gameobj commanding)
            (get-contained-commands gameobj-get-contained-commands)

            (get-occupants gameobj-get-occupants)
            (add-occupant! gameobj-add-occupant!)
            (remove-occupant! gameobj-remove-occupant!)
            (get-loc gameobj-act-get-loc)
            (set-loc! gameobj-act-set-loc!)
            (get-name gameobj-get-name)
            (set-name! gameobj-act-set-name!)
            (get-desc gameobj-get-desc)
            (get-prop gameobj-act-get-prop)
            (goes-by gameobj-act-goes-by)
            (visible-name gameobj-visible-name)
            (self-destruct gameobj-act-self-destruct)
            (tell gameobj-tell-no-op)
            (assist-replace gameobj-act-assist-replace)
            (ok-to-drop-here? (lambda (gameobj message . _)
                                #t)) ; ok to drop by default
            (ok-to-be-taken-from? gameobj-ok-to-be-taken-from)
            (ok-to-be-put-in? gameobj-ok-to-be-put-in)

            ;; Common commands
            (cmd-look-at cmd-look-at)
            (cmd-take cmd-take)
            (cmd-drop cmd-drop)
            (cmd-take-from cmd-take-from-no-op)
            (cmd-put-in cmd-put-in-no-op))))


;;; gameobj message handlers
;;; ========================

;; TODO: This init stuff is a mess, and should be redone now that
;;   we have the *init* action stuff.  We've really spread out the
;;   logic for creating a gameobj in several places, eg gm-inject-special!
(define (create-gameobj class gm loc . args)
  "Create a gameobj of CLASS with GM and set to location LOC, applying rest of ARGS.
Note that this doesn't do any special dyn-ref of the location."
  (let ((new-gameobj (apply create-actor class
                            #:gm gm args)))
    ;; Set the location
    (<-wait new-gameobj 'set-loc! #:loc loc)
    ;; Initialize the object
    (<-wait new-gameobj 'init)))

;; ;; @@: Should we also dyn-ref the loc here?  We can do that, unlike with
;; ;;   create-gameobj.
;; ;;   Another route could be to have set-loc! itself know how to use the
;; ;;   dyn-ref.
;; (define (gameobj-create-gameobj gameobj class loc . args)
;;   "Like create-gameobj but saves the step of passing in the gm."
;;   (apply create-gameobj class (gameobj-gm gameobj) loc args))

;; Kind of a useful utility, maybe?
(define (simple-slot-getter slot)
  (lambda (actor message)
    (slot-ref actor slot)))

(define (gameobj-replace-step-occupants actor occupants)
  ;; Snarf all the occupants!
  (display "replacing occupant\n")
  (when occupants
    (for-each
     (lambda (occupant)
       (<-wait occupant 'set-loc!
               #:loc (actor-id actor)))
     occupants)))

(define gameobj-replace-steps*
  (list gameobj-replace-step-occupants))

(define (run-replacement actor replaces replace-steps)
  (when replaces
    (call-with-values
        (lambda ()
          (<-wait replaces 'assist-replace))
      (lambda* (#:key occupants)
        (for-each
         (lambda (replace-step)
           (replace-step actor occupants))
         replace-steps)))))

(define %nothing (cons '*the* '*nothing*))
(define (gameobj-setup-props gameobj)
  (define class (class-of gameobj))
  (define props (slot-ref gameobj 'props))
  (maybe-build-rmeta-slot-cache! class 'initial-props
                                 eq? hashq-set! hashq-ref)
  ;; Kind of a kludge... we read through the rmeta-slot-cache
  ;; and use that to build up the table
  (hash-for-each
   (lambda (key value)
     (when (eq? (hashq-ref props key %nothing) ; don't override init'ed instance values
                %nothing)
       (hashq-set! props key value)))
   (rmeta-slot-cache (class-slot-ref class 'initial-props))))

;; TODO: Use the *init* action?
;;   We could also use a generic method if they didn't have
;;   what I'm pretty sure is O(n) dispatch in GOOPS...
(define* (gameobj-act-init actor message #:key replace)
  "Your most basic game object init procedure."
  (gameobj-setup-props actor)
  (run-replacement actor replace gameobj-replace-steps*))

(define* (gameobj-get-prop gameobj key #:optional dflt)
  (hashq-ref (slot-ref gameobj 'props) key dflt))

(define* (gameobj-set-prop! gameobj key val)
  (hashq-set! (slot-ref gameobj 'props) key val))

(define* (gameobj-act-get-prop actor message key #:optional dflt)
  (gameobj-get-prop actor key dflt))

(define (gameobj-goes-by gameobj)
  "Find the name we go by.  Defaults to #:name if nothing else provided."
  (cond ((slot-ref gameobj 'goes-by) =>
         identity)
        ((slot-ref gameobj 'name) =>
         (lambda (name)
           (list name)))
        (else '())))

(define (gameobj-act-goes-by actor message)
  "Reply to a message requesting what we go by."
  (gameobj-goes-by actor))

(define (val-or-run val-or-proc)
  "Evaluate if a procedure, or just return otherwise"
  (if (procedure? val-or-proc)
      (val-or-proc)
      val-or-proc))

(define (get-candidate-commands actor rmeta-sym verb)
  (class-rmeta-ref (class-of actor) rmeta-sym verb
                   #:dflt '()))

(define* (gameobj-get-commands actor message #:key verb)
  "Get commands a co-occupant of the room might execute for VERB"
  (define candidate-commands
    (get-candidate-commands actor 'commands verb))
  (values #:commands candidate-commands
          #:goes-by (gameobj-goes-by actor)))

(define* (gameobj-get-container-dom-commands actor message #:key verb)
  "Get (dominant) commands as the container / room of message's sender"
  (define candidate-commands
    (get-candidate-commands actor 'container-dom-commands verb))
  (values #:commands candidate-commands))

(define* (gameobj-get-container-sub-commands actor message #:key verb)
  "Get (subordinate) commands as the container / room of message's sender"
  (define candidate-commands
    (get-candidate-commands actor 'container-sub-commands verb))
  (values #:commands candidate-commands))

(define* (gameobj-get-contained-commands actor message #:key verb)
  "Get commands as being contained (eg inventory) of commanding gameobj"
  (define candidate-commands
    (get-candidate-commands actor 'contained-commands verb))
  (values #:commands candidate-commands
          #:goes-by (gameobj-goes-by actor)))

(define* (gameobj-add-occupant! actor message #:key who)
  "Add an actor to our list of present occupants"
  (hash-set! (slot-ref actor 'occupants)
             who #t))

(define* (gameobj-remove-occupant! actor message #:key who)
  "Remove an occupant from the room."
  (hash-remove! (slot-ref actor 'occupants) who))

(define* (gameobj-occupants gameobj #:key exclude)
  (hash-fold
   (lambda (occupant _ prev)
     (define exclude-it?
       (match exclude
         ;; Empty list and #f are non-exclusion
         (() #f)
         (#f #f)
         ;; A list of addresses... since our address object is (annoyingly)
         ;; currently a simple cons cell...
         ((exclude-1 ... exclude-rest)
          (member occupant exclude))
         ;; Must be an individual address!
         (_ (equal? occupant exclude))))
     (if exclude-it?
         prev
         (cons occupant prev)))
   '()
   (slot-ref gameobj 'occupants)))

(define* (gameobj-get-occupants actor message #:key exclude)
  "Get all present occupants of the room."
  (gameobj-occupants actor #:exclude exclude))

(define (gameobj-act-get-loc actor message)
  (slot-ref actor 'loc))

(define (gameobj-set-loc! gameobj loc)
  "Set the location of this object."
  (define old-loc (gameobj-loc gameobj))
  (format #t "DEBUG: Location set to ~s for ~s\n"
          loc (actor-id-actor gameobj))

  (when (not (equal? old-loc loc))
    (slot-set! gameobj 'loc loc)
    ;; Change registation of where we currently are
    (if old-loc
        (<-wait old-loc 'remove-occupant! #:who (actor-id gameobj)))
    (if loc
        (<-wait loc 'add-occupant! #:who (actor-id gameobj)))))

;; @@: Should it really be #:id ?  Maybe #:loc-id or #:loc?
(define* (gameobj-act-set-loc! actor message #:key loc)
  "Action routine to set the location."
  (gameobj-set-loc! actor loc))

(define (slot-ref-maybe-runcheck gameobj slot whos-asking . other-args)
  "Do a slot-ref on gameobj, evaluating it including ourselves
and whos-asking, and see if we should just return it or run it."
  (match (slot-ref gameobj slot)
    ((? procedure? slot-val-proc)
     (apply slot-val-proc gameobj whos-asking other-args))
    (anything-else anything-else)))

(define gameobj-get-name (simple-slot-getter 'name))

(define* (gameobj-act-set-name! actor message val)
  (slot-set! actor 'name val))

(define* (gameobj-desc gameobj #:key whos-looking)
  (match (slot-ref gameobj 'desc)
    ((? procedure? desc-proc)
     (desc-proc gameobj whos-looking))
    (desc desc)))

(define* (gameobj-get-desc actor message #:key whos-looking)
  "This is the action equivalent of the gameobj-desc getter"
  (gameobj-desc actor #:whos-looking whos-looking))

(define (gameobj-visible-to-player? gameobj whos-looking)
  "Check to see whether we're visible to the player or not.
By default, this is whether or not the generally-visible flag is set."
  (not (slot-ref gameobj 'invisible?)))

(define* (gameobj-visible-name actor message #:key whos-looking)
  ;; Are we visible?
  (define we-are-visible
    ((slot-ref actor 'visible-to-player?) actor whos-looking))

  (define name-to-return
    (if we-are-visible
        ;; Return our name
        (match (slot-ref actor 'name)
          ((? procedure? name-proc)
           (name-proc actor whos-looking))
          ((? string? name)
           name)
          (#f #f))
        #f))
  (values #:text name-to-return))

(define (gameobj-self-destruct gameobj)
  "General gameobj self destruction routine"
  ;; Unregister from being in any particular room
  (gameobj-set-loc! gameobj #f)
  (slot-set! gameobj 'destructed #t)
  ;; Boom!
  (self-destruct gameobj))

(define* (gameobj-act-self-destruct gameobj message #:key why)
  "Action routine for self destruction"
  (gameobj-self-destruct gameobj))

;; Unless an actor has a tell message, we just ignore it
(define gameobj-tell-no-op
  (const 'no-op))

(define (gameobj-replace-data-occupants gameobj)
  "The general purpose list of replacement data"
  (list #:occupants (hash-map->list (lambda (occupant _) occupant)
                                    (slot-ref gameobj 'occupants))))

(define (gameobj-replace-data* gameobj)
  ;; For now, just call gameobj-replace-data-occupants.
  ;; But there may be more in the future!
  (gameobj-replace-data-occupants gameobj))

;; So sad that objects must assist in their replacement ;_;
;; But that's life in a live hacked game!
(define (gameobj-act-assist-replace gameobj message)
  "Vanilla method for assisting in self-replacement for live hacking"
  (apply values (gameobj-replace-data* gameobj)))

(define (gameobj-ok-to-be-taken-from gameobj message whos-acting)
  (call-with-values (lambda ()
                      (slot-ref-maybe-runcheck gameobj 'take-me?
                                               whos-acting #:from #t))
    ;; This allows this to reply with #:why-not if appropriate
    (lambda args args)))

(define (gameobj-ok-to-be-put-in gameobj message whos-acting where)
  (call-with-values (lambda ()
                      (slot-ref-maybe-runcheck gameobj 'drop-me?
                                               whos-acting where))
    ;; This allows this to reply with #:why-not if appropriate
    (lambda args args)))


;;; Utilities every gameobj has
;;; ---------------------------

(define (dyn-ref gameobj special-symbol)
  "Dynamically look up a special object from the gm"
  (match special-symbol
    ;; if it's a symbol, look it up dynamically
    ((? symbol? _)
     ;; TODO: If we get back an #f at this point, should we throw
     ;;   an error?  Obviously #f is okay, but maybe not if 
     (<-wait (slot-ref gameobj 'gm) 'lookup-special
             #:symbol special-symbol))
    ;; if it's false, return nothing
    (#f #f)
    ;; otherwise it's probably an address, return it as-is
    (_ special-symbol)))



;;; Basic actions
;;; -------------

(define %formless-desc
  "You don't see anything special.")

(define* (cmd-look-at gameobj message
                      #:key direct-obj
                      (player (message-from message)))
  (let ((desc
         (or (gameobj-desc gameobj #:whos-looking player)
             %formless-desc)))
    (<- player 'tell #:text desc)))

(define* (cmd-take gameobj message
                   #:key direct-obj
                   (player (message-from message)))
  (define player-name
    (<-wait player 'get-name))
  (define player-loc
    (<-wait player 'get-loc))
  (define our-name (slot-ref gameobj 'name))
  (define self-should-take
    (slot-ref-maybe-runcheck gameobj 'take-me? player))
  ;; @@: Is there any reason to allow the room to object in the way
  ;;   that there is for dropping?  It doesn't seem like it.
  (call-with-values (lambda ()
                      (slot-ref-maybe-runcheck gameobj 'take-me? player))
    (lambda* (self-should-take #:key (why-not
                                      `("It doesn't seem like you can take "
                                        ,our-name ".")))
      (if self-should-take
          ;; Set the location to whoever's picking us up
          (begin
            (gameobj-set-loc! gameobj player)
            (<- player 'tell
                #:text (format #f "You pick up ~a.\n"
                               our-name))
            (<- player-loc 'tell-room
                #:text (format #f "~a picks up ~a.\n"
                               player-name
                               our-name)
                #:exclude player))
          (<- player 'tell #:text why-not)))))

(define* (cmd-drop gameobj message
                   #:key direct-obj
                   (player (message-from message)))
  (define player-name
    (<-wait player 'get-name))
  (define player-loc
    (<-wait player 'get-loc))
  (define our-name (slot-ref gameobj 'name))
  (define should-drop
    (slot-ref-maybe-runcheck gameobj 'drop-me? player))
  (define (room-objection-to-drop)
    
    (receive* (drop-ok? #:key why-not) ; does the room object to dropping?
        (<-wait player-loc 'ok-to-drop-here? player (actor-id gameobj))
      (and (not drop-ok?)
           ;; Either give the specified reason, or give a boilerplate one
           (or why-not
               `("You'd love to drop " ,our-name
                 " but for some reason it doesn't seem like you can"
                 " do that here.")))))
  (cond
   ((not player-loc)
    (<- player 'tell
        #:text `("It doesn't seem like you can drop " ,our-name 
                " here, because you don't seem to be anywhere?!?")))
   ;; TODO: Let ourselves supply a reason why not.
   ((not should-drop)
    (<- player 'tell
        #:text (format #f "It doesn't seem like you can drop ~a.\n"
                       our-name)))
   ((room-objection-to-drop)
    (<- player 'tell
        #:text room-objection-to-drop))
   (else
    (gameobj-set-loc! gameobj player-loc)
    ;; TODO: Allow more flavortext here.
    (<- player 'tell
        #:text (format #f "You drop ~a.\n"
                       our-name))
    (<- player-loc 'tell-room
        #:text (format #f "~a drops ~a.\n"
                       player-name
                       our-name)
        #:exclude player))))

(define* (cmd-take-from-no-op gameobj message
                              #:key direct-obj indir-obj preposition
                              (player (message-from message)))
  (<- player 'tell
      #:text `("It doesn't seem like you can take anything "
               ,preposition " "
               ,(slot-ref gameobj 'name) ".")))

(define* (cmd-put-in-no-op gameobj message
                           #:key direct-obj indir-obj preposition
                           (player (message-from message)))
  (<- player 'tell
      #:text `("It doesn't seem like you can put anything "
               ,preposition " "
               ,(slot-ref gameobj 'name) ".")))
